using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using DG.Tweening;

public class ShowSomething : MonoBehaviour, IInteractable
{
    [Header("想要一直展示")]
    [SerializeField] private bool alwaysShow;
    [Header("想要显示的目标")]
    [SerializeField] private GameObject target;
    
    [Header("想要展示的时间")]
    public float showTime;
    
    private BoxCollider2D _coll;
    private bool satrtShow;
    private bool cantHide;

    private void Awake()
    {
        _coll = target.GetComponent<BoxCollider2D>();
    }

    private void OnTriggerStay2D(Collider2D other)
    {
        target.GetComponent<SpriteRenderer>().DOColor(new Color(1, 1, 1, 1), 0.5f);
    }

    private void OnTriggerExit2D(Collider2D other)
    {
        if(!satrtShow && !cantHide)
            target.GetComponent<SpriteRenderer>().DOColor(new Color(1, 1, 1, 0), 0.5f);
    }

    public void InteractAction()
    {
        if (!alwaysShow)
        {
            satrtShow = true;
            // _showTime = showTime;
            StartCoroutine(Show());
        }
        else
        {
            cantHide = true;
            _coll.enabled = true;
        }
    }

    private IEnumerator Show()
    {
        _coll.enabled = true;
        yield return new WaitForSeconds(showTime);
        _coll.enabled = false;
        target.GetComponent<SpriteRenderer>().DOColor(new Color(1, 1, 1, 0), 0.5f);
        satrtShow = false;
    }
}
